package com.zac.coroutine.algorithm.leetcode.linkedlist

/**
 * author zac
 * date 2024/11/7
 * 83. 删除排序链表中的重复元素
 * --简单--
 * 相关标签
 * 相关企业
 * 给定一个已排序的链表的头 head ， 删除所有重复的元素，使每个元素只出现一次 。返回 已排序的链表 。
 */
class Test10 {


    companion object {

        @JvmStatic
        fun main(args: Array<String>) {
            val node5 = ListNode(null, 4)
            val node4 = ListNode(node5, 3)
            val node3 = ListNode(node4, 3)
            val node2 = ListNode(node3, 2)
            val node1 = ListNode(node2, 1)
            val head = ListNode(node1,1)

            val result = deleteDuplicates(head)

            var next: ListNode? = result
            while (next != null) {
                println(next.`val`)
                next = next.next
            }

        }

        fun deleteDuplicates(head: ListNode?): ListNode? {
            var cur = head
            while (cur?.next != null) {
                if (cur.`val` == cur.next?.`val`) {
                    cur.next = cur.next?.next
                } else {
                    cur = cur.next
                }
            }
            return head
        }

    }


}

